package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.AppAdmin;
import com.service.AppAdminService;
import com.util.PasswordUtil;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/appadmin/")
public class AppAdminAction {

    @Autowired
    private AppAdminService appAdminService ;

    /**
     * 查询app用户信息
     *
     * @return
     */
    @RequestMapping("/getAPP_Admin")
    public ResponseBean adminRole(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10",name = "limit") int size, String text){
        IPage iPage =  appAdminService.findRoleByAdmin(page,size,text);
//        System.out.println(iPage.getRecords());
        return new ResponseBean(0, iPage.getTotal(),iPage.getRecords());
    }

    @RequestMapping("/updAPPAdminStatus")
    public ResponseBean updAPPAdminStatus(String id, Integer status){

        AppAdmin admin = new AppAdmin();
        admin.setId(id);
        admin.setStatus(status);
        if(appAdminService.updateById(admin)){
            return new ResponseBean(0,1,"操作成功","");
        }
        return new ResponseBean(1,1,"操作失败","");
    }

    /**
     * 新增app用户
     * @param appAdmin
     * @return
     */
    @RequestMapping("/addUser")
    public ResponseBean addUser(@RequestBody AppAdmin appAdmin){

        if(StringUtil.isNull(appAdmin.getUsername()) || StringUtil.isNull(appAdmin.getPassword())){
            return new ResponseBean(1,1,"用户名或密码不能为空","");
        }
        if(StringUtil.isNull(appAdmin.getMail()) ){
            return new ResponseBean(1,1,"邮箱不能为空","");
        }
        AppAdmin appAdmin1 = appAdminService.getOne(new QueryWrapper<AppAdmin>().and(wrapper->wrapper.eq("username",appAdmin.getUsername()).or().eq("mail",appAdmin.getMail())));
        if(appAdmin1 != null){
            return new ResponseBean(1,1,"用户名或邮箱不能重复!","");
        }
        appAdmin.setPassword(PasswordUtil.jiami(appAdmin.getPassword()));
        appAdmin.setCreate_time(new Date());
        if(appAdminService.save(appAdmin)){
            return new ResponseBean(0,1,"操作成功","");
        }
        return new ResponseBean(1,1,"操作失败","");
    }
    /**
     * app用户修改密码
     * @return
     */
    @RequestMapping("update_pwd")
    public ResponseBean updatePwd(String opwd , String npwd , String id){
        if(StringUtil.isNull(opwd)) {
            return ResponseBean.error("旧密码不能为空") ;
        }
        if(StringUtil.isNull(npwd)) {
            return ResponseBean.error("新密码不能为空") ;
        }
        String res = appAdminService.appUpdatePassword(opwd , npwd , id) ;
        return res == null ? ResponseBean.OK : ResponseBean.error(res) ;
    }

    /**
     * app用户重置密码
     * @return
     */
    @RequestMapping("resetPassword")
    public ResponseBean resetPassword(String id){
        AppAdmin appAdmin = new AppAdmin();
        appAdmin.setId(id);
        appAdmin.setPassword(PasswordUtil.jiami("123456"));
        if(appAdminService.updateById(appAdmin)){
            return  ResponseBean.OK;
        }
        return   ResponseBean.error("重置密码失败") ;
    }

    /**
     * 根据用户ID获取用户信息
     * @param id
     * @return
     */
    @RequestMapping("getAppAdminInfo")
    public ResponseBean getAppAdminInfo (String id) {
        AppAdmin appAdmin = appAdminService.getById(id);
        if (null == appAdmin) {
            return ResponseBean.error("用户不存在");
        }
        return new ResponseBean(200, "成功", appAdmin);
    }
}
